﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OrderingEntities;
using RaisingStudio.Data.Providers;
using System.IO;

namespace SmartDeviceProjectWinCE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DataContext dataContext;
        public DataContext DataContext
        {
            get
            {
                if (this.dataContext == null)
                {
                    string databaseFileName = Path.Combine((System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)), "ordering.db");
                    string connectionString = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", databaseFileName);
                    this.dataContext = new DataContext("SQLite", connectionString);
                }
                return this.dataContext;
            }
        }

        private int index;

        private void button1_Click(object sender, EventArgs e)
        {
            FoodOrders foodOrder = new FoodOrders();
            foodOrder.TableNO = "NO.1";
            foodOrder.KindName = "Fast Food";
            foodOrder.FoodName = string.Format("Hamburger No.{0}", ++index);
            foodOrder.Price = 10.00F;
            foodOrder.FoodCount = 1;
            foodOrder.CreateTime = System.DateTime.Now;
            foodOrder.UserName = "user1";
            DataContext.Insert<FoodOrders>(foodOrder);

            RefreshDataGrid();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            DataTable dataTable = DataContext.QueryForDataTable<FoodOrders>();
            this.dataGrid1.DataSource = dataTable;
        }
    }
}